import {
  getAllWarehouseOptionList,
  getNetworkWarehouse,
  getStorePageSelect,
  updateLogisticsByIds,
} from '@/api/store'
import { ref } from 'vue'

/**
 * 获取网点仓库
 */
export const useGetNetworkWarehouse = () => {
  // 获取仓库加载
  const getWarehouseLoading = ref(false)

  // 仓库信息
  const warehouse = ref<WarehouseByNetowrkId>({
    address: '',
    area: '',
    city: '',
    enableStore: false,
    province: '',
    storeId: '',
    storeName: '',
    userPhone: '',
    username: '',
  })

  /**
   * 获取仓库
   * @param id - 网点id
   */
  const getWarehouse = async (id: string) => {
    getWarehouseLoading.value = true
    const res = await getNetworkWarehouse(id)
    warehouse.value = res.data.data.row
    getWarehouseLoading.value = false
  }

  return {
    warehouse,
    getWarehouseLoading,
    getWarehouse,
  }
}

/**
 * 获取仓库下拉数据
 */
export const useGetWarehouseOptionList = () => {
  // 获取仓库下拉数据加载
  const getWarehouseOptionListLoading = ref(false)

  // 获取仓库下拉数据请求参数
  const getWarehouseOptionListParam = ref({
    name: '',
    page: 1,
    limit: 100,
    id: '',
  })

  // 仓库下拉数据
  const warehouseOptionList = ref<Array<WarehouseOption>>([])

  /**
   * 获取下拉数据
   */
  const getWarehouseOptionList = async () => {
    getWarehouseOptionListLoading.value = true
    const res = await getStorePageSelect(getWarehouseOptionListParam.value)
    warehouseOptionList.value = res.data.data.rows
    getWarehouseOptionListLoading.value = false
  }

  return {
    warehouseOptionList,
    getWarehouseOptionListParam,
    getWarehouseOptionListLoading,
    getWarehouseOptionList,
  }
}

/**
 * 旧件批量返仓
 */
export const useOldPartMultipleReturn = () => {
  // 提交加载
  const submitLoading = ref(false)

  // 提交表单
  const submitForm = ref<StorageAPIRequest.OldPartMultipleReturn>({
    ids: [],
    logisticsCode: '',
    logisticsCorpName: '',
  })

  /**
   * 提交方法
   */
  const submit = async () => {
    submitLoading.value = true
    const res = await updateLogisticsByIds(submitForm.value)
    submitLoading.value = false
    return res.data
  }

  /**
   * 重置表单
   */
  const resetForm = () => {
    submitForm.value = {
      ids: [],
      logisticsCode: '',
      logisticsCorpName: '',
    }
  }

  return {
    submitForm,
    submitLoading,
    submit,
    resetForm,
  }
}

/**
 * 获取所有的仓库下拉选项
 */
export const useGetAllWarehouseOptionList = () => {
  // 获取下拉选项加载
  const getOptionListLoading = ref(false)

  // 获取下拉选项请求参数
  const getOptionListParam = ref({
    id: '',
    name: '',
    page: 1,
    limit: 1000,
  })

  // 下拉选项列表
  const optionList = ref<Array<AllWarehouseOption>>([])

  /**
   * 获取下拉选项
   */
  const getOptionList = async () => {
    getOptionListLoading.value = true
    const res = await getAllWarehouseOptionList(getOptionListParam.value)
    getOptionListLoading.value = false
    if (res.data.code === 20000) {
      optionList.value = res.data.data.rows
      return ''
    }
    return res.data.message
  }

  return {
    optionList,
    getOptionListParam,
    getOptionListLoading,
    getOptionList,
  }
}
